package com.imy.lua.udmapping;

import com.immomo.mls.utils.ErrorUtils;
import com.immomo.mls.utils.convert.ConvertUtils;
import com.immomo.mls.wrapper.ILuaValueGetter;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaUserdata;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.utils.LuaApiUsed;

import java.util.List;

/**
 * @ProjectName: LuaScript
 * @Package: com.imy.lua.ud
 * @ClassName: UDList
 * @Description: 描述
 * @Author: Imy
 * @CreateDate: 2021/4/19 18:06
 */
@LuaApiUsed
public class UDList extends LuaUserdata<List> {
  public static final String LUA_CLASS_NAME = "List";

  public UDList(Globals g, List jud) {
    super(g, jud);
  }


  @LuaApiUsed
  public LuaValue get(int index){
    index--;
    if (index < 0 || index >= javaUserdata.size()) {
      ErrorUtils.debugLuaError("The index out of range!", globals);
      return null;
    }
    return ConvertUtils.toLuaValue(getGlobals(), javaUserdata.get(index));
  }
  protected UDList(long L, LuaValue[] v) {
    super(L, v);
  }

  public static final ILuaValueGetter<UDList, List> G = new ILuaValueGetter<UDList, List>() {
    @Override
    public UDList newInstance(Globals g, List obj) {
      return new UDList(g, obj);
    }
  };

}